﻿Imports System.Xml

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class IgolderData
    Inherits System.Windows.Forms.UserControl

    'UserControl1 overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    'Public cGambar As System.Drawing.Graphics

    Public myPen As Pen = New Pen(Brushes.DarkMagenta, 1)
    Public myFont As Font = New System.Drawing.Font("Verdana", 6)
    Public myBrush As Brush = New Drawing.SolidBrush(Color.DarkOrchid)


    Public mata_uang As String = "USD"
    Public satuan As String = "oz"
    Public waktu As String = "MONTH"
    Public tipe_garis As String = "straight"
    Public tipe_warna_garis As Color = Color.Black
    Public tipe_warna_background As Color = Color.White

    Private def_matauang As mataUang = mataUang.USD
    Private def_satuan As sat = sat.oz
    Private def_waktu As tipeWaktu = tipeWaktu.MONTH
    Private def_garis As tipeGaris = tipeGaris.straight




    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.canvas = New System.Windows.Forms.PictureBox()
        Me.ProgressBar1 = New System.Windows.Forms.ProgressBar()
        CType(Me.canvas, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'canvas
        '
        Me.canvas.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
            Or System.Windows.Forms.AnchorStyles.Left) _
            Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.canvas.BackColor = System.Drawing.SystemColors.ButtonFace
        Me.canvas.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch
        Me.canvas.Location = New System.Drawing.Point(9, 30)
        Me.canvas.Name = "canvas"
        Me.canvas.Size = New System.Drawing.Size(695, 381)
        Me.canvas.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
        Me.canvas.TabIndex = 13
        Me.canvas.TabStop = False
        '
        'ProgressBar1
        '
        Me.ProgressBar1.Location = New System.Drawing.Point(7, 5)
        Me.ProgressBar1.Name = "ProgressBar1"
        Me.ProgressBar1.Size = New System.Drawing.Size(697, 19)
        Me.ProgressBar1.TabIndex = 14
        '
        'IgolderData
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.AutoSize = True
        Me.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
        Me.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange
        Me.BackColor = System.Drawing.SystemColors.ControlLight
        Me.Controls.Add(Me.ProgressBar1)
        Me.Controls.Add(Me.canvas)
        Me.Name = "IgolderData"
        Me.RightToLeft = System.Windows.Forms.RightToLeft.Yes
        Me.Size = New System.Drawing.Size(707, 435)
        CType(Me.canvas, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

   Public Sub gambarGrafik()

        'mengatur progress bar
        ProgressBar1.Maximum = 100
        ProgressBar1.Minimum = 0


        'Dim aa As Integer = 0

        Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode
        Dim time As Integer

        Dim arrayDate(20000) As String
        Dim arrayValue(20000) As Double

        Dim tmp_satuan As String
        tmp_satuan = satuan
        Dim tmp_mata_uang As String
        tmp_mata_uang = mata_uang
        Dim tmp_waktu As String
        tmp_waktu = ""
        Select Case waktu
            Case "DAY"
                tmp_waktu = "hours=24"
                time = 7
            Case "WEEK"
                tmp_waktu = "hours=168"
                time = 6
            Case "MONTH"
                tmp_waktu = "months=1"
                time = 3
        End Select
        m_xmld = New XmlDocument()
        m_xmld.Load("https://" + "www.igolder.com/GoldData.ashx?type=Historical&&" + tmp_waktu + "&&currency=" + tmp_mata_uang + "&&tz=SE%20Asia%20Standard%20Time&unit=" + tmp_satuan + "&&output=xml")
        m_nodelist = m_xmld.SelectNodes("/GoldQuotes/Price")

        Dim i As Integer
        i = 0

        Dim total_node As Integer = m_nodelist.Count

        For Each m_node In m_nodelist
            arrayDate(i) = m_node.Attributes.GetNamedItem("Date").Value
            arrayValue(i) = m_node.Attributes.GetNamedItem("Value").Value * 100
            i = i + 1
            'Label1.Text = i / total_node * 100
            ProgressBar1.Value = i / total_node * 100
            'Label1.Refresh()
        Next
        Dim stopw As New Stopwatch
        stopw.Start()
        Do
            'Label1.Text = stopwatch.Elapsed.Milliseconds
            'Label1.Refresh()
        Loop Until stopw.Elapsed.Milliseconds = 2000
        stopw.Stop()
        Dim date_selected(time, 2) As String
        date_selected(0, 0) = arrayDate(0)
        date_selected(0, 1) = arrayValue(0)

        Dim split_one() As String
        Dim split_two() As String
        Dim array_bawah(time) As String

        Dim a As Integer
        Dim b As Integer
        Dim c As Integer
        Dim d As Integer
        b = 1
        c = 1
        d = 1

        If (time = 7) Then
            split_one = Split(arrayDate(0), " ")
            split_two = Split(split_one(1), ":")
            array_bawah(0) = split_one(1)
            a = Convert.ToInt32(split_two(0)) - 4
            If (a < 0) Then
                a = a + 24
            End If
            While (c < time + 1)
                split_one = Split(arrayDate(b), " ")
                split_two = Split(split_one(1), ":")
                array_bawah(c) = split_one(1)
                If (split_two(0) = a) Then
                    date_selected(c, 0) = arrayDate(b)
                    date_selected(c, 1) = arrayValue(b)
                    c = c + 1
                    a = a - 3
                    If (a < 0) Then
                        a = a + 24
                    End If
                End If
                b = b + 1
            End While
        ElseIf (time = 6) Then
            split_one = Split(arrayDate(0), " ")
            split_two = Split(split_one(0), "-")
            array_bawah(0) = split_one(0)
            a = Convert.ToInt32(split_two(2))
            While (c < time + 1)
                split_one = Split(arrayDate(b), " ")
                split_two = Split(split_one(0), "-")
                array_bawah(c) = split_one(0)
                If (split_two(2) <> a) Then
                    date_selected(c, 0) = arrayDate(b)
                    date_selected(c, 1) = arrayValue(b)
                    c = c + 1
                    a = split_two(2)
                End If
                b = b + 1
            End While
        ElseIf (time = 3) Then
            split_one = Split(arrayDate(0), " ")
            split_two = Split(split_one(0), "-")
            array_bawah(0) = split_one(0)
            a = Convert.ToInt32(split_two(2)) - 7
            If (a < 0) Then
                If (split_two(1) = 1) Or (split_two(1) = 2) Or (split_two(1) = 4) Or (split_two(1) = 6) Or (split_two(1) = 8) Or (split_two(1) = 9) Or (split_two(1) = 11) Then
                    a = a + 31
                ElseIf (split_two(1) = 3) And (split_two(0) Mod 4 = 0) Then
                    a = a + 29
                ElseIf (split_two(1) = 3) And (split_two(0) Mod 4 <> 0) Then
                    a = a + 28
                Else
                    a = a + 30
                End If
                d = a - 7
            End If
            While (c < time + 1)
                split_one = Split(arrayDate(b), " ")
                split_two = Split(split_one(0), "-")
                array_bawah(c) = split_one(0)
                If (d < 0) Then
                    If (split_two(1) = 1) Or (split_two(1) = 2) Or (split_two(1) = 4) Or (split_two(1) = 6) Or (split_two(1) = 8) Or (split_two(1) = 9) Or (split_two(1) = 11) Then
                        d = d + 31
                    ElseIf (split_two(1) = 3) And (split_two(0) Mod 4 = 0) Then
                        d = d + 29
                    ElseIf (split_two(1) = 3) And (split_two(0) Mod 4 <> 0) Then
                        d = d + 28
                    Else
                        d = d + 30
                    End If
                End If
                If (d > a) Then
                    If (split_two(2) > a) Or (split_two(2) < d) Then
                        date_selected(c, 0) = arrayDate(b)
                        date_selected(c, 1) = arrayValue(b)
                        c = c + 1
                        a = a - 8
                        d = d - 8
                    End If
                Else
                    If (split_two(2) < a) And (split_two(2) > d) Then
                        date_selected(c, 0) = arrayDate(b)
                        date_selected(c, 1) = arrayValue(b)
                        c = c + 1
                        a = a - 8
                        d = d - 8
                    End If
                End If
                b = b + 1
            End While
        End If

        Dim total_interval As Integer
        total_interval = 5
        total_interval = total_interval - 1

        Dim sorting_array(time) As Double
        For i = 0 To time
            sorting_array(i) = Convert.ToDouble(date_selected(i, 1))
        Next


        Array.Sort(sorting_array)

        Dim interval As Integer = 0
        If ((sorting_array(time) - sorting_array(0)) Mod total_interval <> 0) Then
            interval = ((sorting_array(time) - sorting_array(0)) - ((sorting_array(time) - sorting_array(0)) Mod total_interval) + total_interval) / total_interval
        Else
            interval = (sorting_array(time) - sorting_array(0)) / total_interval
        End If

        Dim interval_array(total_interval) As Double
        For i = 0 To (total_interval)
            interval_array(i) = (interval * i) + sorting_array(0)
        Next

        Dim titik_array(time) As Integer
        For i = 0 To time
            titik_array(i) = Convert.ToDouble(date_selected(i, 1))
            titik_array(i) = (((interval_array(total_interval) - titik_array(i)) * (canvas.Height - 35 - ((canvas.Height - 25 - 10)) / 5)) / (interval * total_interval)) + 10
            titik_array(i) = Convert.ToInt32(titik_array(i))

        Next

        Dim panjang_grafik As Integer
        If (canvas.Width - 10 Mod 56 <> 0) Then
            panjang_grafik = canvas.Width - 110 - ((canvas.Width - 110) Mod 56)
        End If

        Dim interval_bawah As Integer
        interval_bawah = panjang_grafik / (time + 1)

        Dim stringFormat As New StringFormat()
        stringFormat.Alignment = StringAlignment.Center
        stringFormat.LineAlignment = StringAlignment.Center

        'gambar background

        Dim MyBackground As Graphics = Controls(Controls.Count - 1).CreateGraphics
        Dim myBackgroundPen As New Pen(Color.White)
        Dim myBackgroundBrush As New SolidBrush(Color.White)
        myBackgroundBrush.Color = tipe_warna_background
        MyBackground.FillRectangle(myBackgroundBrush, 0, 0, 720, 400)



        'Dim gambar = e.Graphics
        Dim gambar As Graphics = Controls(Controls.Count - 1).CreateGraphics

        'gambar sumbu x dan sumbu y
        Dim LineChart As New Pen(Color.Violet, 1)
        gambar.DrawLine(LineChart, 75, 10, 75, canvas.Height - 25)
        gambar.DrawLine(LineChart, 75, canvas.Height - 25, panjang_grafik + 100, canvas.Height - 25)

        'gambar grafik
        Select Case tipe_garis
            Case "straight"
                myPen = New Pen(Color.Black, 1)
                myPen.Color = tipe_warna_garis
            Case "dashed"
                myPen = New Pen(Color.Black, 3)
                myPen.DashStyle = Drawing.Drawing2D.DashStyle.Dash
                myPen.Color = tipe_warna_garis

        End Select
        For i = 4 To 0 Step -1
            gambar.DrawString(Convert.ToString(interval_array(i) / 100), myFont, myBrush, 10, 10 + ((4 - i) * (canvas.Height - 25 - 10) / 5))
        Next

        For i = time To 0 Step -1
            Dim stopwatch As New Stopwatch
            stopwatch.Start()
            If (i > 0) Then
                gambar.DrawLine(myPen, ((panjang_grafik + 100) - (interval_bawah * (i + 1))), titik_array(i), ((panjang_grafik + 100) - (interval_bawah * i)), titik_array(i - 1))
            End If
            gambar.FillEllipse(myBrush, New Rectangle(((panjang_grafik + 100) - (interval_bawah * (i + 1))) - 4, titik_array(i) - 4, 8, 8))
            gambar.DrawString(array_bawah(i), myFont, myBrush, ((panjang_grafik + 100) - (interval_bawah * (i + 1))), canvas.Height - 15, stringFormat)
            Do
                'Label1.Text = stopwatch.Elapsed.Milliseconds
                'Label1.Refresh()
            Loop Until stopwatch.Elapsed.Milliseconds = 300
            stopwatch.Stop()
        Next

    End Sub

    Public Enum mataUang
        USD = 0
        EUR = 1
        GBP = 2
        JPY = 3
    End Enum

    Public Sub uang(ByVal mataUang)
        Select Case mataUang
            Case 0
                mata_uang = "USD"
            Case 1
                mata_uang = "EUR"
            Case 2
                mata_uang = "GBP"
            Case 3
                mata_uang = "JPY"
        End Select
        gambarGrafik()
    End Sub

    Property Currency As mataUang
        Get
            Return def_matauang
        End Get
        Set(value As mataUang)
            uang(value)
            def_matauang = value
        End Set
    End Property

    Public Enum sat
        oz = 0
        gr = 1
    End Enum

    Public Sub pilih_satuan(ByVal sat)
        Select Case sat
            Case 0
                satuan = "oz"
            Case 1
                satuan = "gr"

        End Select
        gambarGrafik()
    End Sub

    Property Weight As sat
        Get
            Return def_satuan
        End Get
        Set(value As sat)
            pilih_satuan(value)
            def_satuan = value
        End Set
    End Property

    Public Enum tipeWaktu
        DAY = 0
        WEEK = 1
        MONTH = 2
    End Enum

    Public Sub pilih_waktu(ByVal isi)
        Select Case isi
            Case 0
                waktu = "DAY"
            Case 1
                waktu = "WEEK"
            Case 2
                waktu = "MONTH"

        End Select
        gambarGrafik()
    End Sub

    Property Time As tipeWaktu
        Get
            Return def_waktu
        End Get
        Set(value As tipeWaktu)
            pilih_waktu(value)
            def_waktu = value
        End Set
    End Property

    Public Enum tipeGaris
        straight = 0
        dashed = 1
    End Enum

    Public Sub pilih_garis(ByVal isi)
        Select Case isi
            Case 0
                tipe_garis = "straight"
            Case 1
                tipe_garis = "dashed"
        End Select
        gambarGrafik()
    End Sub

    Property Line As tipeGaris
        Get
            Return def_garis
        End Get
        Set(value As tipeGaris)
            pilih_garis(value)
            def_garis = value
        End Set
    End Property

    Property changeFont() As Font
        Get
            Return myFont
        End Get
        Set(value As Font)
            myFont = value
            gambarGrafik()
        End Set
    End Property

    Property changeLineColor() As Color
        Get
            Return tipe_warna_garis
        End Get
        Set(value As Color)
            tipe_warna_garis = value
            gambarGrafik()
        End Set
    End Property
    Property changeBackgroundColor() As Color
        Get
            Return tipe_warna_background
        End Get
        Set(value As Color)
            tipe_warna_background = value
            gambarGrafik()
        End Set
    End Property

    Friend WithEvents canvas As System.Windows.Forms.PictureBox
    Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar

End Class